<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="https://www.thymeleaf.org">
<head>
    <title>桌游管理</title>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" media="all" href="/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" media="all" href="/layui/css/layui.css">
    <script type="text/javascript" src="/js/jquery-3.6.0.js"></script>
    <script type="text/javascript" src="/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/layui/layui.js"></script>
</head>
<script>
    function updateValues(ID){
        $("#UpdateID").val(ID);
    }
    function deleteValues(ID){
        $("#DeleteID").val(ID);
    }
    function searchBCount() {
        var bCount = $("#selectMost").val();
        data = {
            bCount: bCount
        }
        $.ajax({
            url: "/selectBCount",
            type: "Post",
            data: data,
            datatype: "html",
            success: function (data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
    }
    function nameBSearch() {
        var name = $("#nameSearch").val();
        data = {
            name: name
        }
        $.ajax({
            url: "/selectBName",
            type: "Post",
            data: data,
            datatype: "html",
            success: function (data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
    }
    function AddBoardgame(){
        var name = $("#AddName").val();
        var introduction = $("#AddIntroduction").val();
        var lowerLimit = $("#AddLowerLimit").val();
        var upperLimit = $("#AddUpperLimit").val();
        var rank = $("#AddRank").val();
        data = {
            name: name,
            introduction: introduction,
            upperLimit: upperLimit,
            lowerLimit: lowerLimit,
            rank: rank,
        }
        $.ajax({
            url:"boardgame/insertBoardgame",
            type: "Post",
            data:data,
            datatype: "html",
            success: function(data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
        setTimeout(function () {
            location.reload();
        }, 3000);
    }
    function WatchBPhoto(ID) {
        data = {
            id: ID
        }
        $.ajax({
            url:"/boardgame/watchBPhoto",
            type: "Post",
            data:data,
            datatype: "html",
            success: function(data, status) {
                $("#KBP").replaceWith(data);
            }
        })
    }
    function UpdateBoardGame(){
        var id = $("#UpdateID").val();
        var rank = parseInt($("#UpdateRank").val());
        if (isNaN(rank) || rank <= 0) {
            rank = 0;
        }
        var shelf = parseInt($("#UpdateShelf").val());
        var row = parseInt($("#UpdateRow").val());
        var column = parseInt($("#UpdateColumn").val());
        var position;
        if(isNaN(shelf) | isNaN(row) || isNaN(column)) {
            position = 0;
        } else {
            position = shelf * 10000 + row * 100 + column;
        }
        data = {
            id: id,
            rank: rank,
            position: position
        }
        $.ajax({
            url:"boardgame/updateBoardgame",
            type: "Post",
            data:data,
            datatype: "html",
            success: function(data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
        setTimeout(function () {
            location.reload();
        }, 3000);
    }
    function DeleteBoardgame(){
        var id = $("#DeleteID").val();
        data = {
            id: id
        }
        $.ajax({
            url:"boardgame/deleteBoardgame",
            type: "Post",
            data:data,
            datatype: "html",
            success: function(data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
        setTimeout(function () {
            location.reload();
        }, 3000);
    }
</script>
<body>
<div class="container mt-3" id="roomInformation">
    <h2>桌游管理界面</h2>
    <br>
    <ul class="nav nav-pills" role="tablist">

        <li class="navbar-form navbar-left" role="search">
            <a class="form-group">
                <input type="text" class="form-control" id="nameSearch" placeholder="搜索名称"/>
            </a>
        </li>

        <li><button type="submit" class="btn btn-default" onclick="nameBSearch()">搜索</button></li>

        <div class="most">
            <select type="button" class="btn btn-light btn-sm" id="selectMost">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>
        </div>

        <li><button type="submit" class="btn btn-default" onclick="searchBCount()">搜索</button></li>

        <li class="pagination"><button type="button" class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#add">添加桌游</button></li>

    </ul>

    <div class="tab-content">
        <div id="home" class="container tab-pane active"><br>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>桌游号</th>
                    <th>桌游名称</th>
                    <th style="width: 444px">桌游介绍</th>
                    <th>人数限制</th>
                    <th>BGG排名</th>
                    <th>摆放位置</th>
                    <th>图片</th>
                    <th>修改</th>
                    <th>删除</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="boardgame:${boardgames}">
                    <td th:text="${boardgame.id}"></td>
                    <td th:text="${boardgame.name}"></td>
                    <td th:text="${boardgame.introduction}"></td>
                    <td th:text="${boardgame.lowerLimit} + '~' + ${boardgame.upperLimit}"></td>
                    <td th:text="${boardgame.rank}"></td>
                    <td th:text="${boardgame.position}"></td>
                    <td><button type="button" class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#photo" th:onclick="|javascript:WatchBPhoto('${boardgame.id}')|">查看</button></td>
                    <td><button type="button" class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#modify" th:onclick="|javascript:updateValues('${boardgame.id}')|">修改</button> </td>
                    <td><button type="button" class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#fire" th:onclick="|javascript:deleteValues('${boardgame.id}')|">删除</button> </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal picture" id="photo">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width:650px">

            <div class="modal-header">
                <h3 class="modal-title">桌游图片</h3>
                <button type="button" class="btn-close" data-bs-dismiss="modal" onclick="location.reload()"></button>
            </div>

            <div class="modal-body" id="KBP">
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="location.reload()">关闭</button>
            </div>

        </div>
    </div>
</div>

<div class="modal Add" id="add">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">

            <div class="modal-header">
                <h3 class="modal-title">桌游信息</h3>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <table class="table table-bordered">
                    <tbody>
                    <tr>
                        <td style="font-size:22px">桌游名称</td><td><input id="AddName" name="AddName" style="height:32px;width:300px;font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">桌游介绍</td><td><input id="AddIntroduction" name="AddIntroduction" style="height:32px;width:100%;font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">人数范围</td><td style="height:32px;font-size:22px"><input id="AddLowerLimit" name="AddLowerLimit" style="height:32px;font-size:22px">~<input id="AddUpperLimit" name="AddUpperLimit" style="height:32px;font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">BGG排名</td><td><input id="AddRank" name="AddRank" style="height:32px;width:300px;font-size:22px"></td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="AddBoardgame()">确定</button>
            </div>

        </div>
    </div>
</div>

<div class="modal alter" id="modify">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width:650px">

            <div class="modal-header">
                <h4 class="modal-title">桌游信息</h4>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <table class="table table-bordered">
                    <thead>
                    </thead>
                    <tbody>
                    <tr>
                        <td style="font-size:22px">桌游号</td><td><input readonly="readonly" id="UpdateID" name="UpdateID" style="height:32px;width:300px;font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">更新排名</td><td><input id="UpdateRank" name="UpdateRank" placeholder="BGG排名" style="height:32px;width:300px;font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">更新位置</td><td style="height:32px;font-size:22px">第<input id="UpdateShelf" name="UpdateShelf" style="height:32px;width:32px;font-size:22px">个架子第<input id="UpdateRow" name="UpdateRow" style="height:32px;width:32px;font-size:22px">行第<input id="UpdateColumn" name="UpdateColumn" style="height:32px;width:32px;font-size:22px">列</td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="UpdateBoardGame()">确定</button>
            </div>

        </div>
    </div>
</div>

<div class="modal Fire" id="fire">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width:650px">

            <div class="modal-header">
                <h3 class="modal-title">删除桌游</h3>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <table class="table table-bordered">
                    <thead>
                    </thead>
                    <tbody>
                    <tr>
                        <td style="font-size:22px">桌游号</td><td><input readonly="readonly" id="DeleteID" name="DeleteID" style="height:32px;width:300px;font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">是否删除该桌游？</td><td style="height:32px;width:300px;font-size:22px">永久删除该桌游（真的很久）</td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="DeleteBoardgame()">是</button>
            </div>

        </div>
    </div>
</div>

</body>
</html>